﻿using ASmile;
using ASmile.Extend;
using ASmile.Helpers;
using ASmile.Extern.Lib.HttpCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASmile.Extern.Lib
{
    public class Global
    {
        public static void PrintLog(string log)
        {
            LogHelper.LogObj.Info(log);
        }

        public static void RunRsp(IEnumerable<IHttpResponse> rsps)
        {
            string url = Config.Settings.Url;
            if (url.IsEmpty())
            {
                url = $"http://*:{Config.Settings.ListenPort}/data/";
            }
            HttpServer server = new HttpServer
            {
                RequestUri = url,
            };
            foreach (var item in rsps)
            {
                server.ResponseList.Add(item);
                LogHelper.LogObj.Info($"加载：{item.Type} 完成");
            }
            server.Start();
        }

        public static void Run(params IHttpResponse[] rsps)
        {
            RunRsp(rsps);
        }

        public static void Run(string cfgFile = null)
        {
            Config.LoadConfig(cfgFile);
            List<IHttpResponse> resp = new List<IHttpResponse>();
            foreach (var item in Config.Settings.ResponseObjects)
            {
                IHttpResponse obj = ReflectionHelper.CreateInstance<IHttpResponse>(item);
                resp.Add(obj);
            }
            RunRsp(resp);
        }
    }
}